package Dao.Daoimpl;

import Dao.Manage;
import Pojo.Msgpojo;
import Pojo.User;
import Pojo.UserMsgpojo;
import utils.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Manageimpl implements Manage {
    Connection conn=null;
//    获取
    @Override
    public List<User> getUserManage() {
        List<User> list=new ArrayList<User>();

        User user = null;
        try {

            conn=jdbc.getconnection();
            String sql="select * from web_user";
            PreparedStatement ps=null;
            ps=conn.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                int id=resultSet.getInt(1);
                String username=resultSet.getString(2);
                String password =resultSet.getString(3);
                int state =resultSet.getInt(4);
                user=new User(id,username,password,state);
                list.add(user);
            }
            jdbc.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
//添加
    @Override
    public boolean addUserManage(User user) {
        try {
            conn=jdbc.getconnection();
            String sql ="insert into web_user value (null ,?,?,?)";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getState());
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
//修改
    @Override
    public boolean updateManage(User user) {
        try {
            conn=jdbc.getconnection();
            String sql ="update web_user set username=?,password=?,state=? where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getState());
            ps.setInt(4,user.getId());
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    //    删除
    @Override
    public boolean delUserManage(int id)  {
        try {
            conn=jdbc.getconnection();
            String sql ="delete from web_user where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    //以上是用户信息表的增删改查
//    下面是管理者的公告增删改

//    改

    @Override
    public boolean updatemsg(Msgpojo msgpojo) {
        try {
            conn=jdbc.getconnection();
            String sql="update msg set header=?,msgtype=?,msgmember=?,msgtext=?,msgtime=now() where mid=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,msgpojo.getHeader());
            ps.setString(2,msgpojo.getMsgtype());
            ps.setString(3,msgpojo.getMsgmember());
            ps.setString(4,msgpojo.getMsgtext());
            ps.setInt(5,msgpojo.getMid());
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
//删
    @Override
    public boolean delmsg(int id) {
        try {
            conn=jdbc.getconnection();
            String sql ="delete from msg where mid=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;

    }
//添加
    @Override
    public boolean addMsgManage(Msgpojo msgpojo) {
        try {
            conn=jdbc.getconnection();
            String sql ="insert into msg value (null,?,?,?,?,now())";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,msgpojo.getHeader());
            ps.setString(2,msgpojo.getMsgtype());
            ps.setString(3,msgpojo.getMsgmember());
            ps.setString(4,msgpojo.getMsgtext());
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public List<UserMsgpojo> getUserMsgpojo()  {
        UserMsgpojo userMsgpojo = null;
        List<UserMsgpojo>ls=new ArrayList<UserMsgpojo>();
        try {
            conn=jdbc.getconnection();
            String sql="select * from usermsg";
            PreparedStatement ps=conn.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                userMsgpojo=new UserMsgpojo(resultSet.getInt(1),resultSet.getString(2),resultSet.getInt(6),resultSet.getString(3),resultSet.getString(4),resultSet.getString(5));
                ls.add(userMsgpojo);
            }
            jdbc.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return ls;

    }

    @Override
    public boolean delusermsg(int id) {
        try {
            conn=jdbc.getconnection();
            String sql="delete from usermsg where id=?";
            PreparedStatement ps =conn.prepareStatement(sql);
            ps.setInt(1,id);
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean updateusermsg(UserMsgpojo userMsgpojo) {
        try {
            conn=jdbc.getconnection();
            String sql="update usermsg set name=?,checkstatus=?,state=?,sex=?,age=? where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,userMsgpojo.getName());
            ps.setString(2,userMsgpojo.getCheckstatus());
            ps.setString(3,userMsgpojo.getState());
            ps.setString(4,userMsgpojo.getSex());
            ps.setInt(5,userMsgpojo.getAge());
            ps.setInt(6,userMsgpojo.getId());
            int i = ps.executeUpdate();
            if (i>=1){
                jdbc.close(conn);
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }


}
